/*
 * Author: nicolas.bredeche(@)lri.fr
 * Created on 2 sept. 2005
 *
 * parameter set repository
 *  
 */

package picoevo.core.evolution;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import picoevo.toolbox.Display;

public class ParameterSet_copy implements Cloneable {

	// ### Data ###

	private ArrayList _keys = new ArrayList();
	private ArrayList _values = new ArrayList();
	private String _name = "(no name)";

	// ### Constructors ###

	public ParameterSet_copy() {
	}

	public ParameterSet_copy(String __name) {
		this._name = __name;
	}

	// ### Main Methods ###

	public void put(String __key, Object __value) {
		_keys.add(__key);
		_values.add(__value);
	}

	public boolean existProperty(String __key) {
		int i;
		for (i = 0; i != _keys.size(); i++)
			if ((((String) _keys.get(i)).compareTo(__key)) == 0)
				break;

		if (i < _keys.size())
			return (true);
		else
			return (false);
	}

	public Object getProperty(String __key) {
		int i;
		for (i = 0; i != _keys.size(); i++)
			if ((((String) _keys.get(i)).compareTo(__key)) == 0)
				break;

		if (i == _keys.size()) {
			Display.info("\nParameterSet \"" + this._name + "\" : key [" + __key + "] does not exist.");
			try {
				throw new Exception();
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.exit(-1);
		}

		return (_values.get(i));
	}

	// ### Other methods ###

	public void put(String __key, int __value) {
		this.put(__key, String.valueOf(__value));
	}

	public void put(String __key, double __value) {
		this.put(__key, String.valueOf(__value));
	}

	public void put(String __key, boolean __value) {
		this.put(__key, String.valueOf(__value));
	}

	public int getIntProperty(String __key) {
		return (Integer.parseInt(this.getProperty(__key).toString()));
	}

	public double getDoubleProperty(String __key) {
		return (Double.parseDouble(this.getProperty(__key).toString()));
	}

	public boolean getBooleanProperty(String __key) {
		String s = this.getProperty(__key).toString();
		if (s == "true" || s == "TRUE")
			return (true);
		else if (s == "false" || s == "FALSE")
			return (false);
		else
			Display.critical(this.getClass().getName() + "::getBooleanProperty(.) - not a boolean");
		return false; // never used.
	}

	public String getStringProperty(String __key) {
		return ((this.getProperty(__key)).toString());
	}

	public void displayInformation() {
		Display.info("\n### PARAMETER SET :\"" + this._name + "\" ###");
		Display.info("Registered properties.\n{begin list}");
		for (int i = 0; i != _keys.size(); i++)
			Display.info("[" + ((String) _keys.get(i)) + "] => [" + _values.get(i) + "]");
		Display.info("{end list}");
	}

	public void displayInformation(String __outputfilename) {
		try {
			BufferedWriter outputFile = new BufferedWriter(new FileWriter(__outputfilename, true));
			outputFile.write("");
			outputFile.write("\n### PARAMETER SET :\"" + this._name + "\" ###\n");
			outputFile.write("Registered properties.\n{begin list}\n");
			for (int i = 0; i != _keys.size(); i++)
				outputFile.write("[" + ((String) _keys.get(i)) + "] => [" + _values.get(i) + "]\n");
			outputFile.write("{end list}\n");
			outputFile.close();
		} catch (IOException e) {
			Display.error("" + this.getClass().getName() + " \"" + this._name + "\" - cannot write logfile [" + __outputfilename + "]");
		}
	}

	/*
	 * public Object clone () { // try // { return super.clone(); // } // catch
	 * (CloneNotSupportedException e) // { // throw new
	 * InternalError(e.toString()); // } }
	 */

	/**
	 * check if all parameters are correctly initialised (debug purpose) -
	 * should be rewritten in inherited classes
	 */
	public void check() {
		/*
		 * int nb = 0; Display.info("\n# Checking Parameters integrity #"); if (
		 * _generations == -1 ) { Display.warning(" number of generations not
		 * set."); nb++; } if ( _initPopSize == -1 ) { Display.warning(" initial
		 * population size not set."); nb++; } if ( _selectionOperator == null ) {
		 * Display.warning(" Selection Operator not set."); nb++; } if (
		 * _initialisationOperator_Population == null ) { Display.warning("
		 * Population Initialisation Operator not set"); nb++; } if (
		 * _initialisationOperator_Individual == null ) { Display.warning("
		 * Individual Initialisation Operator not set"); nb++; } if (
		 * _evaluationOperator_Individual == null ) { Display.warning("
		 * Individual Evaluation Operator not set."); nb++; } if (
		 * _statisticsOperator_Population == null ) { Display.warning("
		 * Population Statistics Operator not set."); nb++; } if ( nb == 0 )
		 * Display.info("ok."); else Display.info(""+nb+" warning(s).");
		 */
	}

	/*
	 * // Lambda/mu parameters int LAMBDAPLUSMU = 0; int LAMBDACOMAMU = 1; //
	 * selection method int TOURNAMENTSELECTION = 0; int RANKSELECTION = 1; int
	 * RANDOMSELECTION = 2; // general-purpose parameters public int
	 * _currentGeneration = 0; public int _maxGenerations = 0; public int
	 * _populationSize = 0;
	 */

	/*
	 * // Lambda/mu parameters static int LAMBDAPLUSMU = 0; static int
	 * LAMBDACOMAMU = 1; // selection method static int TOURNAMENTSELECTION = 0;
	 * static int RANKSELECTION = 1; static int RANDOMSELECTION = 2;
	 */

}
